﻿Imports System.Net
Imports System.Drawing

Public Class AlertWindowForm
    Private Shared OpenedWindowCount As Integer
    Private Shared ClosedWindowCount As Integer
    Private Shared SyncObject As New Object

    Private WithEvents Timer As New Timers.Timer
    Private LiveProgram As LiveProgram

    Public Enum AlertPosition
        None
        LowerRight
        UpperRight
        LowerLeft
        UpperLeft
    End Enum


    Public Sub New(ByVal program As LiveProgram, ByVal position As AlertPosition)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        LiveProgram = DirectCast(program.Clone, LiveProgram)

        Dim margin = 3
        Dim count As Integer = 0
        SyncLock SyncObject
            OpenedWindowCount += 1
            count = OpenedWindowCount
        End SyncLock

        Dim rect = Screen.PrimaryScreen.WorkingArea

        Dim left As Integer
        Dim top As Integer


        Dim width = Me.Bounds.Width + margin
        Dim height = Me.Height + margin

        Select Case position
            Case AlertPosition.LowerLeft, AlertPosition.UpperLeft
                left = width * ((count - 1) \ (rect.Height \ height))
            Case AlertPosition.LowerRight, AlertPosition.UpperRight
                left = rect.Width - width * (((count - 1) \ (rect.Height \ height)) + 1)
        End Select

        Select Case position
            Case AlertPosition.LowerLeft, AlertPosition.LowerRight
                top = rect.Height - height * (((count - 1) Mod (rect.Height \ height)) + 1)
            Case AlertPosition.UpperLeft, AlertPosition.UpperRight
                top = height * ((count - 1) Mod (rect.Height \ height))
        End Select

        Me.Left = left
        Me.Top = top

        ' Live program
        Dim content = System.Text.RegularExpressions.Regex.Replace(program.Description, "[\n\r]+\s*[\n\r]+", vbCrLf, System.Text.RegularExpressions.RegexOptions.Singleline)
        Me.TitleLabel.Text = program.Title
        Me.ContentLabel.Text = content
        Me.StationNameLabel.Text = program.Station.Name
        Me.IconPictureBox.ErrorImage = Me.IconPictureBox.InitialImage
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog

        Try
            Dim client = New WebClient
            Dim stream = client.OpenRead(program.Station.IconUri)
            Dim bmp = New Bitmap(stream)
            stream.Close()
            Me.IconPictureBox.Image = bmp
        Catch ex As Exception
            Me.IconPictureBox.Image = Me.IconPictureBox.InitialImage
        End Try

        Timer.Interval = 10000
        Timer.Start()
    End Sub

    Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer.Elapsed
        Me.Close()
    End Sub

    Private Sub AlertForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SyncLock SyncObject
            ClosedWindowCount += 1
            If OpenedWindowCount = ClosedWindowCount Then
                OpenedWindowCount = 0
                ClosedWindowCount = 0
            End If
        End SyncLock
    End Sub

    Private Sub AlertForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If (e.Button And Windows.Forms.MouseButtons.Left) = 0 Then
            Exit Sub
        End If

        Dim uri = LiveProgram.Uri
        Try
            Process.Start(uri.ToString)
        Catch ex As Exception

        End Try
        Me.Close()
    End Sub

    Private Sub AlertForm_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
        Timer.Stop()
    End Sub

    Private Sub AlertForm_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        Timer.Start()
    End Sub

End Class